package demo4.controller;

import demo4.mapper.IUserMapper;
import demo4.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Scanner;

@Component
public class UserController {
    @Autowired
    private IUserMapper userMapper;
    public void start(){
        /*
        System.out.println("UserController:开始处理用户请求");
        System.out.println("UserController:开始处理注册");
        System.out.println("UserController:获取用户输入信息...");
        userMapper.insert(null);//将数据插入表中
        System.out.println("UserController:注册成功");
         */
        Scanner scanner = new Scanner(System.in);
        System.out.println("请选择操作：1.注册 2.登录 3.展示用户列表 4.修改");
        int num = scanner.nextInt();
        switch (num){
            case 1:
                userReg();
                break;
            case 2:
                userLogin();
                break;
            case 3:
                userList();
                break;
            case 4:
                userUpdate();
                break;
            default:
                System.out.println("输入错误");
                break;
        }
    }
    private void userReg(){
        System.out.println("开始注册");
        User user = new User();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名：");
        user.setUsername(scanner.nextLine());
        System.out.println("请输入密码：");
        user.setPassword(scanner.nextLine());
        System.out.println("请输入昵称：");
        user.setNickname(scanner.nextLine());
        System.out.println("请输入年龄：");
        user.setAge(scanner.nextInt());
        System.out.println(user);
        if(userMapper.selectByUsername(user.getUsername())==null){
            int result = userMapper.insert(user);
            System.out.println(result>0?"注册成功！":"注册失败！");
        }else{
            System.out.println("用户已存在！");
        }
    }
    private void userLogin(){
        System.out.println("开始登录");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username = scanner.nextLine();
        System.out.println("请输入密码：");
        String password = scanner.nextLine();
        User user = userMapper.selectByUsername(username);
        if(user!=null){
            if(user.getPassword().equals(password)){
                System.out.println("登录成功！");
            }else{
                System.out.println("密码错误！");
            }
        }else{
            System.out.println("用户不存在！");
        }
    }
    private void userList(){
        System.out.println("展示用户列表");
        for(User user:userMapper.selectAll()){
            System.out.println(user);
        }
    }
    private void userUpdate(){
        System.out.println("修改密码");
        User user = new User();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名：");
        user.setUsername(scanner.nextLine());
        System.out.println("请输入新密码：");
        user.setPassword(scanner.nextLine());
        System.out.println("请输入新昵称");
        user.setNickname(scanner.nextLine());
        System.out.println("请输入年龄：");
        user.setAge(scanner.nextInt());
        int result = userMapper.update(user);
        System.out.println(result>0?"修改成功！":"修改失败！");
    }
}
